import Vue from "vue";
import VueRouter from "vue-router";

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

Vue.use(VueRouter);

const routes = [
  // 项目启动后默认显示登录界面
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "Login",
    // component: (resolve) => require(["@/views/login"], resolve),
    component: () => import("@/views/qcs/login"),
  },
  {
    // 登录成功后进入管理页面的首页
    path: "/index",
    name: "Index",
    component: () => import("@/views/qcs/index"),
  },
];
const router = new VueRouter({
  mode: "hash",
  // mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("mvToken");
  if (!token && to.path !== "/login") {
    next({ path: "/login", replace: true });
  } else if (token) {
    if (to.path === "/login") {
      next({ path: "/index" });
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;
